import { useSelector } from 'react-redux';
import { Navigate, Route, Routes, useLocation } from 'react-router-dom';
import { Page } from './constant/Page';
import Account from './page/account/Account';
import AddressManagement from './page/addressManagement/AddressManagement';
import Announcement from './page/announcement/Announcement';
import AnnouncementDetail from './page/announcementDetail/AnnouncementDetail';
import ContactUs from './page/contactUs/ContactUs';
import Register from './page/authRegister/Register';
import SignIn from './page/authSignIn/SignIn';
import ForgetPassword from './page/autoForgetPassword/ForgetPassword';
import SignOut from './page/authSignOut/SignOut';
import TermsOfUse from './page/TermsOfUse/TermsOfUse';
import LandingAsk from './page/landingAsk/LandingAsk';
import OrderAll from './page/orderAll/OrderAll';
import OrderAsk from './page/orderAsk/OrderAsk';
import OrderAskDetail from './page/orderAskDetail/OrderAskDetail';
import Property from './page/property/Property';
import Deposit from './page/deposit/Deposit';
import DepositDetail from './page/depositDetail/DepositDetail';
import Record from './page/record/Record';
import Privacy from './page/privacy/Privacy';
import Report from './page/report/Report';
import Support from './page/support/Support';
import TradeAsk from './page/tradeAsk/TradeAsk';
import MyTrade from './page/myTrade/MyTrade';
import Trading from './page/trading/Trading';
import TradingConfirm from './page/tradingConfirm/TradingConfirm';
import EmailVerification from './page/emailVerification/EmailVerification';
import EnableTfa from './page/enableTfa/EnableTfa';
import BankAccount from './page/bankAccount/BankAccount';
import Withdrawal from './page/withdrawal/Withdrawal';
import WithdrawalDetail from './page/withdrawalDetail/WithdrawalDetail';
import Download from './page/download/Download';
import { RootState } from './redux/store';

const AppRoutes = () => {
  const { isLogin } = useSelector((state: RootState) => state.auth);
  const location = useLocation();
  return (
    <Routes>
      <Route path={Page.Landing} element={<LandingAsk />} />
      <Route path={Page.TermsOfUse} element={<TermsOfUse />} />
      <Route path={Page.Privacy} element={<Privacy />} />
      {!isLogin && (
        <>
          <Route path={Page.SignIn} element={<SignIn />} />
          <Route path={Page.Register} element={<Register />} />
          <Route path={Page.ForgetPassword} element={<ForgetPassword />} />
          <Route path={Page.Verification} element={<EmailVerification />} />
          <Route
            path="*"
            element={<Navigate to={Page.SignIn} state={{ from: location.pathname }} />}
          />
        </>
      )}
      {isLogin && (
        <>
          <Route path={Page.Announcement} element={<Announcement />} />
          <Route path={`${Page.Announcement}/:id`} element={<AnnouncementDetail />} />
          <Route path={Page.Trade} element={<TradeAsk />} />
          <Route path={Page.MyTrade} element={<MyTrade />} />
          <Route path={Page.Trading} element={<Trading />} />
          <Route path={Page.TradingConfirm} element={<TradingConfirm />} />
          <Route path={Page.Order} element={<OrderAsk />} />
          <Route path={Page.OrderAll} element={<OrderAll />} />
          <Route path={`${Page.Order}/:orderId`} element={<OrderAskDetail />} />
          <Route path={Page.Property} element={<Property />} />
          <Route path={Page.Deposit} element={<Deposit />} />
          <Route path={`${Page.Deposit}/:recordId`} element={<DepositDetail />} />
          <Route path={Page.Record} element={<Record />} />
          <Route path={Page.Account} element={<Account />} />
          <Route path={Page.CreateAccount} element={<BankAccount />} />
          <Route path={Page.Withdrawal} element={<Withdrawal />} />
          <Route path={`${Page.Withdrawal}/:recordId`} element={<WithdrawalDetail />} />
          <Route path={Page.AddressManagement} element={<AddressManagement />} />
          <Route path={Page.Report} element={<Report />} />
          <Route path={Page.Support} element={<Support />} />
          <Route path={Page.ContactUs} element={<ContactUs />} />
          <Route path={Page.EnableTfa} element={<EnableTfa />} />
          <Route path={Page.SignOut} element={<SignOut />} />
          <Route path={Page.Download} element={<Download />} />
        </>
      )}
      <Route path="*" element={<Navigate to={Page.Landing} />} />
    </Routes>
  );
};

export default AppRoutes;
